//
//  SetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
import Kingfisher
import PhotosUI

struct MyBubbleView: View {
    @State var roleId: String
    @StateObject var viewModel = MyBubbleViewModel()
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
                    .frame(maxWidth: .infinity,maxHeight: .infinity)
            } else {
                GeometryReader { geometry in
                    let spacing: CGFloat = 10 // 行间距和列间距
                    let columns = Array(repeating: GridItem(.flexible(), spacing: spacing), count: 3) // 列间距
                    // 计算单元格宽度（总宽度减去列间距）
                    let itemWidth = (geometry.size.width - spacing * 2) / 3
                    VStack{
                        ScrollView {
                            //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                            LazyVGrid(columns: columns, spacing: spacing) {
                                ForEach(Array(viewModel.items.enumerated()), id: \.element.objectId) { index, item in
                                    MyBubbleItemView(roleId: roleId, bean: item, itemWidth: itemWidth, isSelected: viewModel.selectedIndex == index)
                                        .contentShape(Rectangle())
                                        .onTapGesture {
                                            viewModel.selectedIndex = index
                                        }
                                }
                            }
                        }
                        .scrollIndicators(.hidden)
                    }
                }
            }
        }.padding(5).background(.bg)
        .navigationTitle("装扮气泡")
            .toolbar {
                // 添加一个右标题按钮
                ToolbarItem(placement: .navigationBarTrailing) {
                    HStack{
                        Text("取消装扮")
                            .foregroundColor(themeManager.themeColor)
                            .font(.system(size: 12))
                            .padding(.horizontal,10)
                            .padding(.vertical,7)
                            .cornerRadius(8)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                showFunctionPopup()
                            }
                    }
                }
            }
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getData(true)
                    viewModel.isInit = true
                }
            }
    }
    
    //功能弹窗
    func showFunctionPopup() {
        SwiftEntryKitWrapper.showBottomPopup { dismiss in
            BubbleChooseDialogView() { clickIndex in
                dismiss()
                switch clickIndex {
                case 0:
                    //聊天气泡
                    viewModel.doBubble(roleId: roleId, id: "", isComment: false)
                    break
                case 1:
                    //评论气泡
                    viewModel.doBubble(roleId: roleId, id: "", isComment: true)
                    break
                default:break
                }
            }
        }
    }
}

//#Preview {
//    MyBubbleView()
//}


//定义预览提供者
struct MyBubbleView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: MyBubbleView(roleId:""))
    }
}
